package com.zhuge_new.Activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.flyco.dialog.listener.OnOperItemClickL;
import com.flyco.dialog.widget.ActionSheetDialog;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.zhuge_new.Adapter.AnearngsAdapter;
import com.zhuge_new.Base.BaseActivity;
import com.zhuge_new.R;
import com.zhuge_new.bean.AnearngsBean;
import com.zhuge_new.bean.ProFitsTypeBean;
import com.zhuge_new.http.StringDialogCallback;
import com.zhuge_new.share.BaseHttp;
import com.zhuge_new.untils.ActivityStack;
import com.zhuge_new.untils.DataParse;
import com.zhuge_new.untils.DataUtilS;
import com.zhuge_new.untils.NumberUtil;
import com.zhuge_new.untils.PreferencesUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.nio.channels.NonWritableChannelException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by lenovo on 2018-09-28.
 */

public class AnearningsActivity extends BaseActivity implements View.OnClickListener {

    private View head;
    private RecyclerView anar_list;
    private boolean isErr = false;
    private AnearngsAdapter anearngsAdapter;
    private String type;
    private String time;
    private TextView same_day_monry;
    private TextView yet_money;
    private TextView may_money;
    private Button btn_withdraw_deposit;
    private TextView money_details,type_earnings;
    private TextView money_type;
    private LinearLayout btn_screen;
    private ProFitsTypeBean proFitsTypeBean;
    private AnearngsBean anearngsBean;
    private String typenum = "false";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_anearning);
        head = View.inflate(baseContext, R.layout.head_anearning, null);
        type = getIntent().getStringExtra("type");
        init_title();
        getData(pageNum, typenum);
        gteEarnings();
        getClassify();

    }


    @Override
    public void init_title() {
        super.init_title();
        TextView tv_title = findViewById(R.id.tv_nav_title);
        ImageView iv_back = findViewById(R.id.iv_nav_back);
        tv_title.setText("我的收益");
        anar_list = findViewById(R.id.anar_list);
        anar_list.setLayoutManager(new LinearLayoutManager(this));
        same_day_monry = head.findViewById(R.id.same_day_monry);
        type_earnings=head.findViewById(R.id.type_earnings);
        yet_money = head.findViewById(R.id.yet_money);
        may_money = head.findViewById(R.id.may_money);
        btn_withdraw_deposit = head.findViewById(R.id.btn_withdraw_deposit);
        money_type = head.findViewById(R.id.money_type);
        money_details = head.findViewById(R.id.money_details);
        btn_screen = head.findViewById(R.id.btn_screen);
        iv_back.setOnClickListener(this);
        btn_withdraw_deposit.setOnClickListener(this);
        btn_screen.setOnClickListener(this);
        if (type.equals("2")){
            type_earnings.setText("您的累计收益");
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_nav_back:
                ActivityStack.getScreenManager().popOneActivity(AnearningsActivity.class);
                break;
            case R.id.btn_withdraw_deposit:
                Intent intent = new Intent(this, PutForwordActivity.class);
                startActivity(intent);
                break;
            case R.id.btn_screen:
                careen_Dialog();
                break;
        }

    }


    private void gteEarnings() {
        OkGo.<String>post(BaseHttp.get_profits)
                .tag(this)
                .params("openId", PreferencesUtils.getString(baseContext, "unionid"))
                .execute(new StringDialogCallback(baseContext) {
                    @Override
                    public void onSuccessResponse(Response<String> response, String msg, String msgCode) {
                        try {
                            JSONObject obj = new JSONObject(response.body());
                            if (obj.getString("msgcode").equals("100")) {
                                JSONObject object = obj.getJSONObject("object");
                                if (type.equals("2")){
                                    String sumProfit=NumberUtil.saveRound(object.getString("sumProfit"),2);
                                    if (sumProfit.equals("00")){
                                        same_day_monry.setText("0.00");
                                    }else {
                                        same_day_monry.setText(sumProfit);
                                    }
                                }else {
                                    String dayProfit=NumberUtil.saveRound(object.getString("dayProfit"),2);
                                    if (dayProfit.equals("00")){
                                        same_day_monry.setText("0.00");
                                    }else {
                                        same_day_monry.setText(dayProfit);
                                    }
                                }
                                String moneyWithdraw=NumberUtil.saveRound(object.getString("moneyWithdraw"),2);
                                if (moneyWithdraw.equals("00")){
                                    may_money.setText("0.00");
                                }else {
                                    may_money.setText(moneyWithdraw);
                                }
                                String withdraw=NumberUtil.saveRound(object.getString("withdraw"),2);
                                if (withdraw.equals("00")){
                                    yet_money.setText("0.00");
                                }else {
                                    yet_money.setText(withdraw);
                                }

                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
    }

    public void getData(int pageNum, String typenum) {
        int Year = DataUtilS.getYear();
        int Month = DataUtilS.getMonth();
        int CurrentDayOfMonth = DataUtilS.getCurrentDayOfMonth();
        if (CurrentDayOfMonth < 10) {
            time = String.valueOf(Year) + "-" + String.valueOf(Month) + "-0" + String.valueOf(CurrentDayOfMonth);
        } else {
            time = String.valueOf(Year) + "-" + String.valueOf(Month) + "-" + String.valueOf(CurrentDayOfMonth);
        }

        if (type.equals("1")) {
            typelist(pageNum);
        } else {
            addlist(pageNum);
        }
    }

    private void typelist(int pageNum){
        if (typenum.equals("false")) {
            OkGo.<String>post(BaseHttp.get_profitsDay)
                    .tag(this)
                    .params("openId", PreferencesUtils.getString(baseContext, "unionid"))
                    .params("page", pageNum)
                    .params("rows", "15")
                    .params("dOrC", "1")
                    .params("time", time)
                    .execute(new StringDialogCallback(baseContext) {
                        @Override
                        public void onSuccessResponse(Response<String> response, String msg, String msgCode) {
                            anearngsBean = (AnearngsBean) new DataParse().getBean(response.body(), AnearngsBean.class, 0);
                            money_details.setText("数量  " + anearngsBean.getTotalNum() + "  金额  " + anearngsBean.getTotalProfit() + " 元");
                            anearngsAdapter = new AnearngsAdapter(baseContext, anearngsBean);
                            anearngsAdapter.addHeaderView(head);
                            anearngsAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
                                @Override
                                public void onLoadMoreRequested() {
                                    loadMore();
                                }
                            }, anar_list);
                            anar_list.setAdapter(anearngsAdapter);
                        }
                    });
        } else {
            OkGo.<String>post(BaseHttp.get_profitsDay)
                    .tag(this)
                    .params("openId", PreferencesUtils.getString(baseContext, "unionid"))
                    .params("page", pageNum)
                    .params("rows", "15")
                    .params("dOrC", "1")
                    .params("type", typenum)
                    .params("time", time)
                    .execute(new StringDialogCallback(baseContext) {
                        @Override
                        public void onSuccessResponse(Response<String> response, String msg, String msgCode) {
                            anearngsBean.getObject().clear();
                            anearngsAdapter.removeAllHeaderView();
                            anearngsBean = (AnearngsBean) new DataParse().getBean(response.body(), AnearngsBean.class, 0);
                            money_details.setText("数量  " + anearngsBean.getTotalNum() + "  金额  " + anearngsBean.getTotalProfit() + " 元");
                            anearngsAdapter = new AnearngsAdapter(baseContext, anearngsBean);
                            anearngsAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
                                @Override
                                public void onLoadMoreRequested() {
                                    loadMore();
                                }
                            }, anar_list);
                            anearngsAdapter.addHeaderView(head);
                            anearngsAdapter.notifyDataSetChanged();
                            anar_list.setAdapter(anearngsAdapter);
                        }
                    });
        }
    }

    private void addlist(int pageNum){
        if (typenum.equals("false")) {
            OkGo.<String>post(BaseHttp.get_profitsDay)
                    .tag(this)
                    .params("openId", PreferencesUtils.getString(baseContext, "unionid"))
                    .params("page", this.pageNum)
                    .params("rows", "15")
                    .params("dOrC", "1")
                    .execute(new StringDialogCallback(baseContext) {
                        @Override
                        public void onSuccessResponse(Response<String> response, String msg, String msgCode) {
                            anearngsBean = (AnearngsBean) new DataParse().getBean(response.body(), AnearngsBean.class, 0);
                            money_details.setText("数量  " + anearngsBean.getTotalNum() + "  金额  " + anearngsBean.getTotalProfit() + " 元");
                            anearngsAdapter = new AnearngsAdapter(baseContext, anearngsBean);
                            anearngsAdapter.addHeaderView(head);
                            anearngsAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
                                @Override
                                public void onLoadMoreRequested() {
                                    addMore();
                                }
                            }, anar_list);
                            anar_list.setAdapter(anearngsAdapter);
                        }
                    });
        } else {
            OkGo.<String>post(BaseHttp.get_profitsDay)
                    .tag(this)
                    .params("openId", PreferencesUtils.getString(baseContext, "unionid"))
                    .params("page", this.pageNum)
                    .params("rows", "15")
                    .params("dOrC", "1")
                    .params("type", typenum)
                    .execute(new StringDialogCallback(baseContext) {
                        @Override
                        public void onSuccessResponse(Response<String> response, String msg, String msgCode) {
                            anearngsBean.getObject().clear();
                            anearngsAdapter.removeAllHeaderView();
                            anearngsBean = (AnearngsBean) new DataParse().getBean(response.body(), AnearngsBean.class, 0);
                            money_details.setText("数量  " + anearngsBean.getTotalNum() + "  金额  " + anearngsBean.getTotalProfit() + " 元");
                            anearngsAdapter = new AnearngsAdapter(baseContext, anearngsBean);
                            anearngsAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
                                @Override
                                public void onLoadMoreRequested() {
                                    addMore();
                                }
                            }, anar_list);
                            anearngsAdapter.addHeaderView(head);
                            anearngsAdapter.notifyDataSetChanged();
                            anar_list.setAdapter(anearngsAdapter);
                        }
                    });
        }
    }

    private void loadMore() {
        pageNum = pageNum + 1;
        OkGo.<String>post(BaseHttp.get_profitsDay)
                .tag(this)
                .params("openId", PreferencesUtils.getString(baseContext, "unionid"))
                .params("page", pageNum)
                .params("rows", "15")
                .params("dOrC", "1")
                .params("time", time)
                .params("type", typenum)
                .execute(new StringDialogCallback(baseContext) {
                    @Override
                    public void onSuccessResponse(Response<String> response, String msg, String msgCode) {
                        try {
                            JSONObject obj = new JSONObject(response.body());
                            AnearngsBean anearngsBean = (AnearngsBean) new DataParse().getBean(response.body(), AnearngsBean.class, 0);
                            anearngsAdapter.addData(anearngsBean.getObject());
                            anearngsAdapter.loadMoreComplete();
                            if (anearngsBean.getObject().isEmpty()) {
                                pageNum = pageNum - 1;
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
    }

    private void addMore() {
        pageNum = pageNum + 1;
        OkGo.<String>post(BaseHttp.get_profitsDay)
                .tag(this)
                .params("openId", PreferencesUtils.getString(baseContext, "unionid"))
                .params("page", pageNum)
                .params("rows", "15")
                .params("dOrC", "1")
                .params("type", typenum)
                .execute(new StringDialogCallback(baseContext) {
                    @Override
                    public void onSuccessResponse(Response<String> response, String msg, String msgCode) {
                        try {
                            JSONObject obj = new JSONObject(response.body());
                            AnearngsBean anearngsBean = (AnearngsBean) new DataParse().getBean(response.body(), AnearngsBean.class, 0);
                            anearngsAdapter.addData(anearngsBean.getObject());
                            anearngsAdapter.loadMoreComplete();
                            if (anearngsBean.getObject().isEmpty()) {
                                pageNum = pageNum - 1;
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
    }

    public void getClassify() {
        OkGo.<String>post(BaseHttp.get_profitsType)
                .tag(this)
                .execute(new StringDialogCallback(baseContext) {
                    @Override
                    public void onSuccessResponse(Response<String> response, String msg, String msgCode) {
                        proFitsTypeBean = (ProFitsTypeBean) new DataParse().getBean(response.body(), ProFitsTypeBean.class, 1);

                    }
                });
    }

    private void careen_Dialog() {

        final ArrayList data = new ArrayList();
        for (int i = 0; i < proFitsTypeBean.getObject().size(); i++) {
            if (i == 0) {
                data.add("全部");
            }
            data.add(proFitsTypeBean.getObject().get(i).getProfitType());
        }
        final String[] strings = (String[]) data.toArray(new String[data.size()]);
        final ActionSheetDialog dialog = new ActionSheetDialog(baseContext, strings, null);
        dialog.cancelText(R.color.black_therec)
                .itemTextSize(14)
                .itemTextColor(R.color.black_therec).isTitleShow(false).show();
        dialog.setOnOperItemClickL(new OnOperItemClickL() {
            @Override
            public void onOperItemClick(AdapterView<?> parent, View view, int position, long id) {
                String type = String.valueOf(data.get(position));
                for (int i = 0; i < proFitsTypeBean.getObject().size(); i++) {
                    if (type.equals(proFitsTypeBean.getObject().get(i).getProfitType()) || type.equals("全部")) {
                        if (type.equals("全部")) {
                            typenum = "0";
                        } else {
                            typenum = proFitsTypeBean.getObject().get(i).getProfitTypeNum();
                        }

                    }
                }
                dialog.dismiss();
                pageNum=1;
                getData(pageNum, typenum);
            }
        });

    }


}
